package ch10;

import java.util.HashMap;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

public class MapClass {

	public static void main(String[] args) {
		//Map集合用于保存有映射关系的数据
		//存储数据时是通过key value的对应关系进行保存
		//key是一个Set集合，不允许有null key，不能保存相同的key；保存key相同的数据，后面的数据会覆盖前面的数据（修改数据）
		//value是一个Collection集合
		//HashMap允许null为key，允许null为value；key不能重复，value可以重复
		//Hashtable不允许null为key，不允许null为value，value可以重复
		
		//HashMap线程不安全，Hashtable线程安全
		Map<String, Computer> map = new HashMap<String, Computer>(16);
		//Map<String, Computer> map = new Hashtable<String, Computer>(16);

		//新增方法
		map.put("HP", new Computer("HP2018",3000));
		map.put("HW", new Computer("HW2009",3000));
		map.put("DELL", new Computer("DELL2020",3000));
		map.put(null, new Computer("DELL2020",3000));


		//遍历map一般先取出key，在遍历key，在根据key取value
		Set<String> keys = map.keySet();
		Iterator<String> iterator = keys.iterator();
		while (iterator.hasNext()) {
			String key = iterator.next();
			Computer computer = map.get(key);
			System.out.println("品牌"+key+"型号"+computer.getBrand()+"价格"+computer.getPrice());
		}
		
		System.out.println("====================================");
		
		//一对多关系
		Map<String, List<Computer>> map2 = new HashMap<String, List<Computer>>();

		TreeMap<String, Computer> map3 = new TreeMap<String, Computer>();
		map3.put("HP", new Computer("HP2018",3000));
		map3.put("HW", new Computer("HW2009",3000));
		map3.put("DELL", new Computer("DELL2020",3000));

		iterator = keys.iterator();
		while (iterator.hasNext()) {
			String key = iterator.next();
			Computer computer = map.get(key);
			if(computer!=null) {
			System.out.println("品牌"+key+"型号"+computer.getBrand()+"价格"+computer.getPrice());
			}
		}
		
	}

}
